package com.zscat.mallplus.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.service.ICmsSubjectService;
import com.zscat.mallplus.pay.utils.StringUtils;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.service.IPmsProductService;
import com.zscat.mallplus.sys.entity.MallplusKeyword;
import com.zscat.mallplus.sys.entity.MallplusSearchHistory;
import com.zscat.mallplus.sys.mapper.MallplusKeywordMapper;
import com.zscat.mallplus.sys.mapper.MallplusSearchHistoryMapper;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.BaseEntity;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: rwz
 * @Date: 2020/10/09/3:22 下午
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "MarketSearchController", description = "搜索")
@RequestMapping("/api/market/search")
public class MarketSearchController {

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private ICmsSubjectService subjectService;

    @Resource
    private IPmsProductService productService;

    @Resource
    private MallplusKeywordMapper mallplusKeywordMapper;

    @Resource
    private MallplusSearchHistoryMapper mallplusSearchHistoryMapper;

    /**
     * 资讯搜索
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @IgnoreAuth
    @ApiOperation(value = "资讯搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @GetMapping(value = "subjectList")
    public Object search(@RequestParam(value = "storeId", required = false) Integer storeId,
    					 @RequestParam(value = "keyword", required = false) String keyword,
                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum) {

        Long memberId = memberService.getNewCurrentMember().getId();
        if (memberId != null) {
            MallplusSearchHistory mallplusSearchHistory = new MallplusSearchHistory();
            mallplusSearchHistory.setAddTime(LocalDateTime.now());
            mallplusSearchHistory.setDeleted(false);
            mallplusSearchHistory.setKeyword(keyword);
            mallplusSearchHistory.setSource(2);
            mallplusSearchHistory.setUpdateTime(LocalDateTime.now());
            mallplusSearchHistory.setUserId(memberId);
            mallplusSearchHistory.setStoreId(storeId);
            mallplusSearchHistoryMapper.insert(mallplusSearchHistory);
        }

        IPage<CmsSubject> cmsSubjectIPage = subjectService.page(
                new Page<CmsSubject>(pageNum, pageSize),
                new QueryWrapper<CmsSubject>()
                        .lambda()
                        .eq(BaseEntity::getStoreId, storeId)
                        .like(StringUtils.isNoneEmpty(keyword), CmsSubject::getTitle, keyword)
                        .orderByDesc(CmsSubject::getCreateTime)
        );

        return new CommonResult().success(cmsSubjectIPage);
    }

    @IgnoreAuth
    @ApiOperation(value = "商品搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productName", value = "商品名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productMajor", value = "商品大类：1->普通商品；2->积分商品", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @SysLog(MODULE = "product", REMARK = "商品搜索")
    @GetMapping(value = "productList")
    public Object productList(@RequestParam(value = "keyword", required = false) String keyword,
                              @RequestParam(value = "productMajor", required = false) Integer productMajor,
                              @RequestParam(value = "storeId", required = false) Integer storeId,
                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        MallplusSearchHistory mallplusSearchHistory = new MallplusSearchHistory();
        mallplusSearchHistory.setAddTime(LocalDateTime.now());
        mallplusSearchHistory.setDeleted(false);
        mallplusSearchHistory.setKeyword(keyword);
        mallplusSearchHistory.setSource(1);
        mallplusSearchHistory.setUpdateTime(LocalDateTime.now());
        mallplusSearchHistory.setUserId(currentMember.getId());
        mallplusSearchHistory.setStoreId(storeId);
        mallplusSearchHistoryMapper.insert(mallplusSearchHistory);
        return new CommonResult().success(productService.page(new Page<PmsProduct>(pageNum, pageSize),
                new QueryWrapper<PmsProduct>().like("name", keyword).eq("product_major", productMajor)
                        .eq("publish_status", 1).eq("store_id", storeId).orderByDesc("create_time")));
    }

    @IgnoreAuth
    @ApiOperation(value = "热门搜索关键字")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "source", value = "来源：1->商品；2->资讯", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @GetMapping(value = "hotKeywordList")
    public Object hotKeywordList(@RequestParam(value = "source", required = true) Integer source,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (ValidatorUtils.empty(source)) {
            return new CommonResult().failed("来源不能为空");
        }
        return new CommonResult().success(mallplusKeywordMapper.selectPage(new Page<MallplusKeyword>(pageNum, pageSize), new QueryWrapper<MallplusKeyword>().eq("source", source).eq("deleted", 0).orderByDesc("add_time")));
    }

    @IgnoreAuth
    @ApiOperation(value = "历史搜索关键字")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "source", value = "来源：1->商品；2->资讯", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @GetMapping(value = "historyKeywordList")
    public CommonResult historyKeywordList(@RequestParam(value = "source", required = true) Integer source,
                                           @RequestParam(value = "storeId", required = false) Integer storeId,
                                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        IPage<MallplusSearchHistory> searchHistoryIPage = mallplusSearchHistoryMapper.selectPage(new Page<MallplusSearchHistory>(pageNum, pageSize),
                new QueryWrapper<MallplusSearchHistory>().eq("store_id", storeId).orderByDesc("add_time").lambda().eq(MallplusSearchHistory::getUserId, currentMember.getId()).eq(MallplusSearchHistory::getSource, source).eq(MallplusSearchHistory::getDeleted, 0));
        return new CommonResult().success(searchHistoryIPage);
    }
}
